package com.windy.kafka.sender.annotation.listener;

import com.windy.kafka.sender.annotation.context.HeaderContextHolder;
import com.windy.kafka.sender.annotation.header.TracingHeaders;
import com.windy.kafka.sender.annotation.vo.People;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.support.KafkaHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

/**
 * Created by liyufeng on 2022/3/24
 */
@Slf4j
@ConditionalOnProperty(value = "com.windy.enable.kafka.listener", havingValue = "true", matchIfMissing = true)
@Component
public class KafkaMessageListener {
    @KafkaListener(topics = "test")
    public void onEventListener(@Payload People msg,
                                @Header(value = KafkaHeaders.RECEIVED_TOPIC, required = false) String topic,
                                @Header(value = KafkaHeaders.RECEIVED_MESSAGE_KEY, required = false) String key,
                                @Header(value = KafkaHeaders.RECEIVED_PARTITION_ID, required = false) Integer partitionId,
                                @Header(value = "x-trace-id", required = false) String traceId) {

        try {
            log.info("receive msg : {} ,topic :{} ,key :{} ,partitionId :{},traceId:{}", msg, topic, key, partitionId, traceId);
            TracingHeaders header = HeaderContextHolder.getHeader();

            log.info("从threadLocal拿到的headers:{}", header);
        } finally {
            HeaderContextHolder.clear();
        }


    }
}
